<?php

namespace app\api\controller;

//这段代码很重要，这样才能允许页面访问接口，要不然会有跨域问题
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Token");
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');
use app\model\Domain as DomainModel;
use app\model\Member as MemberModel;
use app\model\Page as PageModel;
use app\model\User as UserModel;
use think\Controller;
use think\facade\Config;
use think\facade\Log;
use think\facade\Request;

class Common extends Controller
{
    /**
     * 用户权限验证
     * @return string
     */
    protected function Auth()
    {

        $token = Request::instance()->header('Token');
        if (!empty($token)) {
            //验证token
            $model = UserModel::getUserByToken($token);
            if ($model) {
                define('USER_ID', $model->id);
                define('USER_NAME', $model->names);
            } else {
                return $this->jsonFail('500', '用户在其它地方登录');
            }
        } else {
            return $this->jsonFail('500', '登录超时');
        }

        // $c_menu = MenuModel::getInfo();
        // if (!$c_menu) {
        //     return $this->error('节点不存在或者已禁用！');
        // }

        // 检查权限
        // if (!RoleModel::checkAuth($c_menu['id'])) {
        //     $url = '';
        //     //如果没有后台首页的登录权限，直接退出，避免出现死循环跳转
        //     if ($c_menu['url'] == 'admin/mian') {
        //         $url =  url('/Admin/index', '', true, true)
        //         model('AdminUser')->logout();
        //     }
        //     return $this->error('['.$c_menu['title'].'] 访问权限不足', $url);
        // }

        // 系统日志记录
        // $log = [];
        // $log['uid'] = USER_ID;
        // $log['title'] = $c_menu['title'];
        // $log['url'] = $c_menu['url'];
        // $log['param'] = json_encode(input('param.'));
        // $log['remark'] = '浏览数据';
        // if ($this->request->isPost()) {
        //     $log['remark'] = '保存数据';
        // }
        // $log_result = LogModel::where($log)->find();
        // $log['ip'] = $this->request->ip();
        // if (!$log_result) {
        //     LogModel::create($log);
        // } else {
        //     $log['id'] = $log_result->id;
        //     $log['count'] = $log_result->count+1;
        //     LogModel::update($log);
        // }
        return false;
    }

    /**
     * 获取图片路经
     * @return string
     */
    protected function getPicPath($picpath)
    {
        $domain = DomainModel::getDomainByTmp('defaults');

        if (!$domain) {
            $domain = Request::domain();
            //$domain = Config::get('domain');

        }
        if ($picpath) {
            return $domain . '/public/uploads/' . $picpath;
        } else {
            return $domain . '/public/static/images/nopicture.jpg';
        }

    }

    /**
     * 获取扩展信息
     * @return string
     */
    protected function getContent($name)
    {
        $content = PageModel::getContentByName($name);
        return $content;
    }

    /**
     * 获取会员信息
     * @return string
     */
    protected function getMember($mid)
    {
        $member = MemberModel::get($mid);
        return $member;
    }
    /**
     * 获取当前方法URL
     * @return string
     */
    protected function getActUrl()
    {
        $model = request()->module();
        $controller = request()->controller();
        $action = request()->action();
        return $model . '/' . $controller . '/' . $action;
    }

    /**
     * 成功
     * @param string $msg 提示信息
     * @param array $data 返回数据
     * @return string
     */
    protected function jsonSuccess($msg = '操作成功', $data)
    {
        $res = [
            'code' => 1,
            'msg' => $msg,
            'data' => $data,
        ];
        return json()->data($res);
    }

    /**
     * 分页
     * @param string $count 提示信息
     * @param array $data 返回数据
     * @return string
     */
    protected function jsonPage($count, $data)
    {
        $res = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $data,
        ];
        return json()->data($res);
    }

    /**
     * 失败
     * @param string $code 错误代码
     * @param array $msg 错误信息
     * @return string
     */
    protected function jsonFail($code = '0', $msg = '操作失败')
    {
        $res = [
            'code' => $code,
            'msg' => $msg,
        ];
        return json()->data($res);
    }

    /**
     * 禁止访问
     * @return string
     */
    protected function jsonStop()
    {
        $res = [
            'code' => 999,
            'msg' => '禁止访问',
        ];
        return json()->data($res);
    }
}
